package admin

import (
	"anban/logic"
	"anban/service"
	"anban/utils"
	"anban/utils/wechat"
	"fmt"
	"github.com/astaxie/beego"
	"html/template"
	"net/url"
	"strconv"
)

type GradeNoticeController struct {
	BaseController
}

func (c *GradeNoticeController) Add() {
	userAdminId := c.GetSession("userAdminId")
	p := map[string]interface{}{}
	p["id"] = userAdminId
	p["relation"] = true
	userAdmin := service.GetUserAdminInfo(p)

	p = map[string]interface{}{}
	p["status"] = 1
	p["schoolId"] = userAdmin.UserAdminRole.School.Id
	_, c.Data["classList"] = service.GetClassList(p)

	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "admin/grade_notice/add.html"
}

func (c *GradeNoticeController) DoAdd() {
	var res *utils.ResJsonStruct
	input := c.Input()

	userAdminId := c.GetSession("userAdminId")
	p := map[string]interface{}{}
	p["id"] = userAdminId
	p["relation"] = true
	userAdmin := service.GetUserAdminInfo(p)

	p = map[string]interface{}{}
	p["id"] = userAdmin.UserAdminRole.School.Id
	school := service.GetSchoolInfo(p)
	if school.IsUseGrade != 1 {
		res = utils.ResJson(0, "学校没有使用成绩通知功能", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	err := logic.AddGrade(input)
	if err != nil {
		res = utils.ResJson(err.Code(), err.Error(), "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	go c.SendGradeWechatNotice(input)

	res = utils.ResJson(1, "发布成功", "")
	c.Data["json"] = res
	c.ServeJSON()
}

func (c *GradeNoticeController) SendGradeWechatNotice(input url.Values) {
	domain := beego.AppConfig.String("domain")
	path := beego.URLFor("parent.GradeNoticeController.ShowList")
	wechatUrl := fmt.Sprintf("%s%s", domain, path)

	p := map[string]interface{}{}
	p["id"] = input["student_id"][0]
	student := service.GetStudentInfo(p)
	if student.Id > 0 && len(student.FamilySn) > 0 {
		p = map[string]interface{}{}
		p["status"] = 1
		p["familySn"] = student.FamilySn
		_, userList := service.GetUserList(p)
		for _, user := range userList {
			if user.SubscribeStatus == 1 {
				wechat.SendGradeNotice(user.OpenId, "您有新的成绩通知", "", wechatUrl, student.RealName, input["test_name"][0], "点击查看孩子成绩")
			}
		}
	}
	return
}

func (c *GradeNoticeController) ExcelAdd() {
	userAdminId := c.GetSession("userAdminId")
	p := map[string]interface{}{}
	p["id"] = userAdminId
	p["relation"] = true
	userAdmin := service.GetUserAdminInfo(p)

	p = map[string]interface{}{}
	p["status"] = 1
	p["schoolId"] = userAdmin.UserAdminRole.School.Id
	_, c.Data["classList"] = service.GetClassList(p)

	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "admin/grade_notice/excel_add.html"
}

func (c *GradeNoticeController) DoExcelAdd() {
	var res *utils.ResJsonStruct
	input := c.Input()

	userAdminId := c.GetSession("userAdminId")
	p := map[string]interface{}{}
	p["id"] = userAdminId
	p["relation"] = true
	userAdmin := service.GetUserAdminInfo(p)

	p = map[string]interface{}{}
	p["id"] = userAdmin.UserAdminRole.School.Id
	school := service.GetSchoolInfo(p)
	if school.IsUseGrade != 1 {
		res = utils.ResJson(0, "学校没有使用成绩通知功能", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	err := logic.AddGradeFromExcel(input)
	if err != nil {
		res = utils.ResJson(err.Code(), err.Error(), "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	res = utils.ResJson(1, "导入成功", "")
	c.Data["json"] = res
	c.ServeJSON()
}

func (c *GradeNoticeController) Edit() {
	id := c.Ctx.Input.Param(":id")

	p := map[string]interface{}{}
	p["id"] = id
	p["relation"] = true
	p["convert"] = true
	c.Data["record"] = service.GetGradeNoticeInfo(p)

	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "admin/grade_notice/edit.html"
}

func (c *GradeNoticeController) DoEdit() {
	var res *utils.ResJsonStruct
	input := c.Input()
	if len(input["test_name"][0]) <= 0 {
		res = utils.ResJson(0, "请输入考试名称", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	_, err := service.EditGradeNotice(input)
	if err != nil {
		res = utils.ResJson(0, "修改失败", "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	res = utils.ResJson(1, "修改成功", "")
	c.Data["json"] = res
	c.ServeJSON()
}

func (c *GradeNoticeController) ShowList() {
	curPage, _ := strconv.Atoi(c.GetString("p"))
	if curPage <= 0 {
		curPage = 1
	}

	perCount, _ := beego.AppConfig.Int("percount")
	symPageCount, _ := beego.AppConfig.Int("symmetricpagecount")

	userAdminId := c.GetSession("userAdminId")
	p := map[string]interface{}{}
	p["id"] = userAdminId
	p["relation"] = true
	userAdmin := service.GetUserAdminInfo(p)

	p = map[string]interface{}{}
	p["schoolId"] = userAdmin.UserAdminRole.School.Id
	p["relation"] = true
	p["convert"] = true
	p["curPage"] = curPage
	p["perCount"] = perCount
	totalCount, recordList := service.GetGradeNoticeList(p)
	paginator := utils.NewPaginator(int(totalCount), perCount, symPageCount, curPage)
	c.Data["paginator"] = paginator.GetPageHtml()
	c.Data["recordList"] = recordList

	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "admin/grade_notice/list.html"
}